<?php
import( 'de.ceus-media.framework.krypton.core.Logic' );
import( 'classes.model.User' );
//import( 'classes.model.Role' );
//import( 'classes.model.UserRole' );
/**
 *	Logic of User, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
/**
 *	Logic of User, Locations and Companies.
 *	@package		mv2.logic
 *	@extends		Framework_Krypton_Core_Logic
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			29.08.2007
 *	@version		0.1
 */
 class Logic_User extends Framework_Krypton_Core_Logic
 {
	/**
	 *	Adds a new user into the database after verifying an unique name and email.
	 *	@access		private
	 *	@param		array		$data			Array of User Data
	 *	@parma		string		$prefix			Prefix of Input Values
	 * 	@param		bool		$testOnly
	 *	@return		bool
	 */
	public function addUser( $data, $prefix = 'add_', $testOnly = FALSE )
	{
		$modelU	= new Model_User();
		$modelRU	= new Model_RoleUser();

		//  --  VALIDATION  --  //
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'account/user', 'addUser', $data, 'add_' );

		if( $data['password'] != $data['confirm'] )
			throw new Framework_Krypton_Exception_Logic( 'error_password_not_equal', $data['confirm'] );

		//  --  CHECK UNIQUE USER NAME  --  //
		$modelU->focusIndex( 'username', $data['username'] );
		if( count( $modelU->get() ) > 0 )
			throw new Framework_Krypton_Exception_Logic( 'error_user_not_unique', $data['username'] );

		//  --  CHECK UNIQUE EMAIL  --  //
		$modelU->defocus();
		$modelU->focusIndex( 'email', $data['email'] );
		if( count( $modelU->get() ) > 0 )
			throw new Framework_Krypton_Exception_Logic( 'error_email_not_unique', $data['email'] );

		//  --  TEST  --  //
		if ( $testOnly )
			return TRUE;

		//  --  ADD USER  --  //
		$data['status']		= 1;
		$data['password']	= md5( $data['password'] );

		$userId	= $modelU->insert( $data );
#		foreach( $data['user_roles'] as $role )
#			$modelRU->insert( array( 'userId' => $userId, 'roleId' => $role, 'creatorId' => $data['creatorId'] ) );
		return $userId;
	}

	/**
	 *	Edits Password with Confirmation, compares with old Password.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@param		array		$data			Data for update
	 *	@param		string		$prefix			Prefix of Input Values
	 *	@return		bool
	 */
	public function editPassword( $userId, $data, $prefix = "edit_" )
	{
		$model	= new Model_User( $userId );
		$user	= $model->get();

		//  --  VALIDATION  --  //
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( 'account/user', 'editPassword', $data, $prefix );

		//  --  CHECK USER ID  --  //
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'error_user_invalid' );

		//  --  CHECK OLD PASSWORD  --  //
		if( md5( $data['password_old'] ) != $user['password'] )
			throw new Framework_Krypton_Exception_Logic( 'error_password_invalid' );

		//  --  CHECK PASSWORD EQUALITY  --  //
		if( $data['password_new'] != $data['password_2nd'] )
			throw new Framework_Krypton_Exception_Logic( 'error_passwords_not_equal' );

		//  --  EDIT PASSWORD  --  //
		$data	= array( 'password'	=> md5( $data['password_new'] ) );
		return $model->update( $data );
	}

	/**
	 *	Edits User Data.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@param		array		$data			Data to update
	 *	@param		string		$prefix			...
	 *	@return		bool
	 */
	public function editUser( $userId, $data, $prefix = "edit_" )
	{
		$model	= new Model_User( $userId );

		//  --  VALIDATION  --  //
		$data	= $this->removePrefixFromFields( $data, $prefix );
		$this->validateForm( "account/user", "editUser", $data, $prefix );

		//  --  CHECK USER ID  --  //
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'error_user_invalid' );

		//  --  CHECK UNIQUE USERNAME  --  //
		$users	= $model->find( array(), array( 'username' => $data['username'], 'userId' => "!=".$userId ) );
		if( count( $users ) )
			throw new Framework_Krypton_Exception_Logic( 'error_user_not_unique', $data['username'] );

		//  --  CHECK UNIQUE MAIL  --  //
		$users	= $model->find( array(), array( 'email' => $data['email'], 'userId' => "!=".$userId ) );
		if( count( $users ) )
			throw new Framework_Krypton_Exception_Logic( 'error_email_not_unique', $data['email'] );

		return $model->update( $data );
	}

	public static function getLicenceeId( $userId )
	{
		$user	= self::getUserData( $userId );
		return $user['licenceeId'];
	}

	/**
	 *	Returns user data by user ID.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		array						User data
	 */
	public static function getUserData( $userId )
	{
		$model		= new Model_User( $userId );
		if ( !$model->exists() )
		{
			throw new Framework_Krypton_Exception_Logic( 'error_user_not_found' );
		}
		return $model->get();
	}

	/**
	 *	Returns User Language by User ID.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		string
	 */
	public static function getUserLanguage( $userId )
	{
		$user	= self::getUserData( $userId );
		return $user['language'];
	}

 	/**
	 *	Returns User Name by User ID.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		string
	 */
	public static function getUserName( $userId )
	{
		$user	= self::getUserData( $userId );
		return $user['username'];
	}

	/**
	 *	Returns User Status by User ID.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@return		int
	 */
	public static function getUserStatus( $userId )
	{
		$user	= self::getUserData( $userId );
		return $user['status'];
	}

	public function removeUser( $userId )
	{
		$model	= new Model_User( $userId );
		if( !$model->exists() )
			throw new Framework_Krypton_Exception_Logic( 'error_user_invalid' );
		$logic	= new Logic_Role();
		if( $logic->removeUserRoles( $userId ) )
			return $model->delete();
	}

	/**
	 *	Sets User Status by User ID.
	 *	@access		public
	 *	@param		int			$userId			User ID
	 *	@param		int			$status			Status to be set
	 *	@return		bool
	 */
	public static function setStatus( $userId, $status )
	{
		$user	= new Model_User( $userId );
		return (bool) $user->update( array( 'status' => $status ) );
	}
}
?>